package com.ai.spring.common.util;

import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * IO辅助类
 *
 * @author 石头
 * @Date 2019/11/18
 * @Version 1.0
 **/
@Slf4j
public class IOUtils {
    private static final int BUFFER_SIZE = 1024 * 8;
    public static String read(Reader reader)throws IOException{
        StringWriter writer = new StringWriter();
        try {
            write(reader, writer);
            return writer.getBuffer().toString();
        } finally {
            writer.close();
        }
    }
    public static long write(Reader reader, Writer writer) throws IOException {
        return write(reader, writer, BUFFER_SIZE);
    }
    public static long write(Reader reader, Writer writer, int bufferSize) throws IOException {
        int read;
        long total = 0;
        char[] buf = new char[BUFFER_SIZE];
        while ((read = reader.read(buf)) != -1) {
            writer.write(buf, 0, read);
            total += read;
        }
        return total;
    }
    public static void closeStream(Closeable stream){
        if (stream!=null){
            try {
                stream.close();
            } catch (IOException e) {
                log.warn("-----关闭流失败:----",e);
            }
        }
    }
}
